//
//  RPNcalcViewController.m
//  RPNcalc
//
//  Created by iD Student on 6/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RPNcalcViewController.h"
#import "RPNcalcBRAIN.h"

@interface RPNcalcViewController ()
@property (nonatomic) BOOL userIsTheMiddleOfEnteringANumber;
@property (nonatomic) RPNcalcBRAIN *brain;
@end

@implementation RPNcalcViewController;

@synthesize display = _display;
@synthesize userIsTheMiddleOfEnteringANumber = _userIsTheMiddleOfEnteringANumber;
@synthesize brain=_brain;
-(RPNcalcBRAIN *)brain
{
    if(!_brain)
    {
        _brain = [[RPNcalcBRAIN alloc] init];
    }
    return _brain;
}

- (IBAction)digitPressed:(UIButton *)sender
{
    NSString *digit = sender.currentTitle;

    if(self.userIsTheMiddleOfEnteringANumber)
    {
        self.display.text = [self.display.text stringByAppendingString:digit];

    }
    else
    {
        
    self.display.text = digit;
        self.userIsTheMiddleOfEnteringANumber = YES;
    }

}
- (IBAction)enterPressed 
{
    [self.brain pushOperand:[self.display.text doubleValue]];
    self.userIsTheMiddleOfEnteringANumber = NO;
}



- (IBAction)operandPressed:(UIButton *)sender
{
    if(self.userIsTheMiddleOfEnteringANumber)
    {
        [self enterPressed];
    }
    
    double result = [self.brain performeOperation: sender.currentTitle];
    NSString * resultString =[NSString stringWithFormat:@"%g",result];
    self.display.text = resultString;
}


@end
